<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item
            label="主标题"
            prop="title"
            :rules="[{ required: true, message: '请选择主标题关联字段', trigger: 'change' }]"
          >
            <el-select v-model="form.title" clearable placeholder="请选择主标题关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="主标题文字大小"
            prop="titleFontSize"
            :rules="[{ required: true, message: '请输入主标题文字大小', trigger: 'blur' }]"
          >
            <el-input-number v-model="form.titleFontSize" :min="12" clearable placeholder="请输入主标题文字大小" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="主标题字体颜色"
            prop="titleFontColor"
            :rules="[{ required: true, message: '请选择主标题字体颜色', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.titleFontColor" clearable placeholder="请选择主标题字体颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="副标题"
            prop="fbt"
          >
            <el-select v-model="form.fbt" clearable placeholder="请选择副标题关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="副标题字体大小"
            prop="fbtFontSize"
          >
            <el-input-number v-model="form.fbtFontSize" :min="12" clearable placeholder="请输入副标题字体大小" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="副标题颜色"
            prop="fbtFontColor"
            style="width: 100%"
          >
            <el-color-picker v-model="form.fbtFontColor" clearable placeholder="请选择副标题颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="左显示项名称"
            prop="leftTitleName"
            style="width: 100%"
          >
            <el-input v-model="form.leftTitleName" clearable placeholder="请输入左显示项名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="左关联字段"
            prop="leftTitle"
          >
            <el-select v-model="form.leftTitle" clearable placeholder="请选择左关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="字体大小"
            prop="leftFontSize"
          >
            <el-input-number v-model="form.leftFontSize" :min="12" clearable placeholder="请输入字体大小" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="字体颜色"
            prop="leftFontColor"
            style="width: 100%"
          >
            <el-color-picker v-model="form.leftFontColor" clearable placeholder="请选择字体颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="右边显示名称"
            prop="rightTitleName"
            style="width: 100%"
          >
            <el-input v-model="form.rightTitleName" clearable placeholder="请输入右边显示名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="右关联字段"
            prop="rightTitle"
          >
            <el-select v-model="form.rightTitle" clearable placeholder="请选择右关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="字体大小"
            prop="rightFontSize"
          >
            <el-input-number v-model="form.rightFontSize" :min="12" clearable placeholder="请输入字体大小" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="字体颜色"
            prop="rightFontColor"
            style="width: 100%"
          >
            <el-color-picker v-model="form.rightFontColor" clearable placeholder="请选择字体颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="内容字段"
            prop="content"
          >
            <el-select v-model="form.content" clearable placeholder="请选择右关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>

<script>
import { updateObject } from '@/api/portal/manager/entityTemp'

export default {
  name: 'NewsPcEntityV1',
  props: {
    column: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      form: {
        title: '', // 主标题
        titleFontSize: '', // 主标题文字大小
        titleFontColor: '', // 主标题字体颜色
        fbt: '', // 副标题
        fbtFontSize: null, // 副标题字体大小
        fbtFontColor: '', // 副标题颜色
        leftTitleName: '', // 左边显示项名称
        leftTitle: '', // 左边关联字段
        leftFontSize: '', // 左边字体大小
        leftFontColor: '', // 左边字体颜色
        rightTitleName: '', // 右边显示名称
        rightTitle: '', // 右边关联字段
        rightFontSize: '', // 右边字体大小
        rightFontColor: '', // 右边字体颜色
        content: '' // 内容关联字段
      },
      params: []
    }
  },
  watch: {
    column: {
      handler(newName, oldName) {
        if (undefined !== newName.jxx && newName.jxx !== null) {
          this.params = JSON.parse(newName.jxx)
        }
        if (undefined !== newName.pzx && newName.pzx !== null) {
          this.form = JSON.parse(newName.pzx)
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.column.pzx = JSON.stringify(this.form)
          updateObject(this.column).then(res => {
            this.$message.success('更新成功！')
            this.close()
          }).catch(() => {
            this.loading = false
            this.$message.error('更新失败！')
          })
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>
.setting-body {
  width: 100%;
}
.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}
.custom-button {
  text-align: right;
}
</style>
